﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Linq;
using System.ComponentModel;
using System.Text;
namespace VCommons
{
    public partial class EmailHelper
    {
        /// <summary>
        /// 是否需要SSL 形式加密
        /// </summary>
        bool enableSsl;
        /// <summary>
        /// 端口号
        /// </summary>
        int? port;

        public EmailHelper()
        {
            enableSsl = false;
        }

        public EmailHelper(bool enableSsl)
        {
            this.enableSsl = enableSsl;
        }

        public EmailHelper(bool enableSsl, int port)
            : this(enableSsl)
        {
            this.port = port;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailServerName">发送邮件的SMTP服务器</param>
        /// <param name="mailFrom">发件人邮箱</param>
        /// <param name="mailTo">收件人邮箱</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件正文</param>
        /// <param name="userName">访问SMTP服务器的用户名</param>
        /// <param name="password">访问SMTP服务器的密码</param>
        /// <param name="cc">获取包含此电子邮件的抄送 (CC) 收件人的地址 可按照索引单独访问的一组对象</param>
        public void Send(
            string mailServerName,
            string mailFrom,
            IList<string> mailTos,
            string subject,
            string body,
            string userName,
            string password,
            IList<string> cc)
        {

            MailMessage message = new MailMessage();
            //获取或设置此电子邮件的主题内容使用的编码
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            //获取或设置此电子邮件的主题行。
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Priority = MailPriority.Normal;
            //获取或设置此电子邮件的发件人地址
            //message.Sender = new MailAddress(mailFrom);

            //获取或设置此电子邮件的发信人地址
            message.From = new MailAddress(mailFrom);

            //获取或设置用于邮件正文的编码
            message.BodyEncoding = System.Text.Encoding.UTF8;
            //获取或设置邮件正文
            message.Body = body;
            //设置收件人地址
            if (mailTos != null && mailTos.Count > 0)
            {
                mailTos.ToList().ForEach(item => message.To.Add(item));
            }
            //增加抄送信息
            if (cc != null && cc.Count > 0)
            {
                cc.ToList().ForEach(item => message.CC.Add(item));
            }

            //SmtpClient是发送邮件的主体，这个构造函数是告知SmtpClient发送邮件时使用哪个SMTP服务器
            SmtpClient mailClient = new SmtpClient(mailServerName);

            //需要ssl端验证
            if (this.enableSsl) { mailClient.EnableSsl = this.enableSsl; }
            if (this.port.HasValue) { mailClient.Port = this.port.Value; }
            //mailClient.UseDefaultCredentials = true;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Credentials = new NetworkCredential(userName, password);

            //最终的发送方法
            try
            {
                mailClient.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}